/*
 *  RegexValidator.cs
 *  
 *  Validates the user input by a specific regular expression.
 *  
 *  Copyright (c) 2009, Richard Bao Labs.
 *  
 *  The contents of this file are subject to the Mozilla Public License
 *  Version 1.1 (the "License"); you may not use this file except in
 *  compliance with the License. You may obtain a copy of the License at
 *  
 *  http://www.mozilla.org/MPL/
 *  
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 *  the License for the specific language governing rights and limitations
 *  under the License.
 *  
 *  The Original Code is RichControlLibrary.
 *  
 *  The Initial Developer of the Original Code is Richard Bao Labs.
 *  All Rights Reserved.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RichControlLibrary
{
    /// <summary>
    /// Validates the user input by a specific regular expression.
    /// </summary>
    public abstract class RegexValidator : Validator
    {
        /// <summary>
        /// Returns a <see cref="Regex"/> instance as the validating regular expression.
        /// </summary>
        /// <returns>A <see cref="Regex"/> used to validate the user input.</returns>
        protected abstract Regex GetValidatingRegex();

        /// <summary>
        /// Validates the specified text by the validating regular expression.
        /// </summary>
        /// <param name="text">The text to validate.</param>
        /// <returns>A <see cref="ValidationResult"/> that contains the validation result.</returns>
        public override ValidationResult Validate(string text)
        {
            ValidationResult result = new ValidationResult();

            Regex regex = GetValidatingRegex();

            Match match = regex.Match(text);
            if (match.Success)
            {
                result.Success = true;

                if (SupportsAutoFormatting)
                {
                    result.AutoCorrectedText = GetAutoFormattedText(text, match);
                }
            }
            else
            {
                result.Success = false;
            }
            
            return result;
        }

        /// <summary>
        /// Gets a <see cref="System.Boolean"/> value that indicates whether the auto formatting feature
        /// is supported by this validator.
        /// </summary>
        /// <remarks>
        /// If returns true, the <see cref="GetAutoFormattedText"/> method must be overridden to return the
        /// auto formatted text.
        /// </remarks>
        public override bool SupportsAutoFormatting
        {
            get { return false; }
        }

        /// <summary>
        /// Gets the auto formatted text if this validator supports auto correction feature.
        /// </summary>
        /// <param name="rawText">The raw text string entered by user.</param>
        /// <param name="match">The matching result of the validating regular expression.</param>
        /// <returns>The auto formatted text.</returns>
        /// <remarks>
        /// When you want to provide the auto formatting feature to this validator, make sure you override 
        /// both this method and <see cref="SupportsAutoFormatting"/> property, and sets its value to true.
        /// The auto formatted text must be also valid to the validating regular expression (returned by
        /// <see cref="GetValidatingRegex"/> method), otherwise you might get unexpected result.
        /// </remarks>
        protected virtual string GetAutoFormattedText(string rawText, Match match)
        {
            return null;
        }
    }
}
